<template>
  <div class="app-container">
    <el-form :inline="true" ref="searchForm" :model="listQuery" class="demo-form-inline">
      <el-form-item>
        <el-button class="btn-add" @click="addProductAttrCate" size="small"> 添加 </el-button>
      </el-form-item>
    </el-form>
    <div class="table-container">
      <el-table ref="productAttrCateTable" height="100%" style="width: 100%" :data="list" v-loading="listLoading" border>
        <el-table-column label="编号" width="80" align="center">
          <template #default="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column label="类型名称" minWidth="160" align="center">
          <template #default="scope">{{ scope.row.name }}</template>
        </el-table-column>
        <el-table-column label="属性数量" width="100" align="center">
          <template #default="scope">{{ scope.row.attributeCount == null ? 0 : scope.row.attributeCount }}</template>
        </el-table-column>
        <el-table-column label="参数数量" width="100" align="center">
          <template #default="scope">{{ scope.row.paramCount == null ? 0 : scope.row.paramCount }}</template>
        </el-table-column>
        <el-table-column label="设置" width="200" align="center">
          <template #default="scope">
            <el-button link size="small" @click="getAttrList(scope.row)">属性列表 </el-button>
            <el-button link size="small" @click="getParamList(scope.row)">参数列表 </el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120" align="center">
          <template #default="scope">
            <el-button link size="small" @click="handleUpdate(scope.row)">编辑 </el-button>
            <el-button link size="small" type="danger" @click="handleDelete(scope.row)">删除 </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :page-size="listQuery.pageSize"
        :page-sizes="[5, 10, 15]"
        v-model:current-page="listQuery.pageNum"
        :total="total"
      >
      </el-pagination>
    </div>
    <el-dialog :title="dialogTitle" v-model="dialogVisible" :before-close="handleClose()" width="30%">
      <el-form ref="productAttrCatForm" :model="productAttrCate" :rules="rules" label-width="120px">
        <el-form-item label="类型名称" prop="name">
          <el-input v-model="productAttrCate.name" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleConfirm">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>
<script setup lang="ts">
import { fetchList, createProductAttrCate, deleteProductAttrCate, updateProductAttrCate } from '@/api/productAttrCate'
import { ref } from 'vue'
import { ElMessage, ElMessageBox, type FormInstance } from 'element-plus'
import { useRouter } from 'vue-router'
import type { PageDto } from '@/models/request'
import type { ProductAttrCate } from '@/models/productAttrCate'
defineOptions({ name: 'ProductAttrCateList' })
const rules = {
  name: [{ required: true, message: '请输入类型名称', trigger: 'blur' }],
}
const productAttrCatForm = ref<FormInstance>()
const router = useRouter()
const list = ref<ProductAttrCate[]>([])
const total = ref(0)
const listLoading = ref(true)
const dialogVisible = ref(false)
const dialogTitle = ref('')
const listQuery = ref<PageDto>({ pageNum: 1, pageSize: 10 })
const productAttrCate = ref<ProductAttrCate>({})

getList()

async function getList() {
  listLoading.value = true
  const response = await fetchList(listQuery.value)
  listLoading.value = false
  list.value = response.data.data.list
  total.value = response.data.data.total
}
function addProductAttrCate() {
  dialogVisible.value = true
  dialogTitle.value = '添加类型'
}
function handleSizeChange(val: number) {
  listQuery.value.pageNum = 1
  listQuery.value.pageSize = val
  getList()
}
function handleCurrentChange(val: number) {
  listQuery.value.pageNum = val
  getList()
}
async function handleDelete(row: ProductAttrCate) {
  await ElMessageBox.confirm('是否要删除该品牌', '提示', { type: 'warning' })
  row.id && (await deleteProductAttrCate(row.id))
  ElMessage.success('删除成功！')
  getList()
}
function handleUpdate(row: ProductAttrCate) {
  dialogVisible.value = true
  dialogTitle.value = '编辑类型'
  productAttrCate.value.name = row.name
  productAttrCate.value.id = row.id
}
function getAttrList(row: ProductAttrCate) {
  router.push({ path: '/pms/productAttr', query: { cid: row.id, cname: row.name, type: 0 } })
}
function getParamList(row: ProductAttrCate) {
  router.push({ path: '/pms/productAttr', query: { cid: row.id, cname: row.name, type: 1 } })
}
async function handleConfirm() {
  await productAttrCatForm.value?.validate()
  const { name, id } = productAttrCate.value
  if (!name) return
  if (id) {
    await updateProductAttrCate(id, { name })
    ElMessage.success('修改成功')
  } else {
    await createProductAttrCate({ name })
    ElMessage.success('添加成功')
  }
  dialogVisible.value = false
  getList()
}
function handleClose() {
  if (!dialogVisible.value && productAttrCatForm.value) {
    productAttrCatForm.value.clearValidate()
  }
}
</script>
<style lang="scss" scoped></style>
